use std::sync::Arc;
use dashmap::DashMap;
use crate::{CmdStrategy, Redis, RespFrame, RespNull, Session, SimpleString};

pub struct GetCmd;


impl CmdStrategy for GetCmd {
    fn execute(&self, fragments: Vec<&str>, db: Redis, sesseion_id: String, sessions: Arc<DashMap<String, Session>>) -> Result<RespFrame, String> {
        let key = fragments[4];
        let s = sessions.get(&sesseion_id);

        let db_index = s.unwrap().db_index;

        let binding = db.databases[db_index].read().unwrap();
        let entry = binding.get(key);

        match entry {
            Some(v) => {
                let res = v;
                println!("v is {}", res);
                let res = SimpleString::new(res);
                Ok(RespFrame::SimpleString(res))
            }

            None => {
                Ok(RespFrame::Null(RespNull))
            }
        }
    }
}